home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: presby.edu!jtbell
- From: jtbell@presby.edu (Jon Bell)
- Subject: Re: How to read: operator overloaded <<
- Message-ID: <Dq7xwK.AK6@presby.edu>
- Date: Sun, 21 Apr 1996 15:33:55 GMT
- References: <4ldfpc$j0u@amanda.dorsai.org>
- Organization: Presbyterian College, Clinton, South Carolina USA
-
- Jeff Yu <mongoose@dorsai.org> wrote:
- >Hi, I have hard time to understand the syntac of << operator overloaded
- >declaration. As it is stated in Lippman's C++ Primer, the << sample is
- >supposed to take TWO parameters: ostream& operator<<(ostream& os, String& s)
- >But when using it, the way is: cout<<"test"<<MyScreen<<endl;
- >Where is the second parameter if I consider the MyScreen as the first one?
-
- The compiler treats binary operator expressions as if they were
- two-parameter function calls. In your case,
-
- cout << "test"
-
- is compiled as if it were
-
- operator<< (cout, "test")
-
- The value returned by this function is the first parameter, cout, which
- allows output operations to be "chained." Therefore your entire output
- statement is compiled as if it were
-
- operator<< ((operator<< ((operator<< (cout, "test")), MyScreen)), endl)
-
- which outputs "test" and reduces to
-
- operator<< ((operator<< (cout, MyScreen)), endl)
-
- which outputs MyScreen and reduces to
-
- operator<< (cout, endl)
-
- which outputs the end of line.
-
- --
- Jon Bell <jtbell@presby.edu> Presbyterian College
- Dept. of Physics and Computer Science Clinton, South Carolina USA
-